package com.xl6667.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xl6667.constants.SystemConstants;
import com.xl6667.domain.ResponseResult;
import com.xl6667.domain.dto.LinkAllDto;
import com.xl6667.domain.dto.PageDto;
import com.xl6667.domain.entity.Link;
import com.xl6667.mapper.LinkMapper;
import com.xl6667.service.LinkService;
import com.xl6667.util.BeanCopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 熊磊
* @description 针对表【sg_link(友链)】的数据库操作Service实现
* @createDate 2024-03-15 19:16:46
*/
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link>
    implements LinkService{

    @Override
    public ResponseResult getAllLink() {

        LambdaQueryWrapper<Link> queryWrapper =new LambdaQueryWrapper<>();
        //审核通过
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> linkList = list(queryWrapper);

        List<LinkAllDto> linkAllDtos = BeanCopyUtil.copyBeanList(linkList, LinkAllDto.class);

        return ResponseResult.okResult(linkAllDtos);
    }

    @Override
    public ResponseResult listLink(Integer pageNum,Integer pageSize,String name,String status) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.hasText(name),Link::getName,name);

        queryWrapper.eq(StringUtils.hasText(status),Link::getStatus,status);

        queryWrapper.eq(Link::getDelFlag,SystemConstants.DEL_FLAG_NORMA);

        Page<Link> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);

        List<Link> records = page.getRecords();
        PageDto pageDto = new PageDto(records,page.getTotal());
        return ResponseResult.okResult(pageDto);
    }

    @Override
    public ResponseResult addLink(Link link) {

        save(link);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult getLink(Long id) {
        Link link = getById(id);
        return ResponseResult.okResult(link);
    }

    @Override
    public ResponseResult putLink(Link link) {
        updateById(link);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult changeStatusLink(Link link) {
        updateById(link);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult delLink(List<Long> ids) {
        LambdaUpdateWrapper<Link> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Link::getId,ids).set(Link::getDelFlag,1);
        update(null,updateWrapper);
        return ResponseResult.okResult("删除成功");
    }
}




